﻿@{
    ViewBag.Title = "购买";
    Layout = "~/Views/Shared/_Layout.cshtml";
}

@Styles.Render("~/Content/css/buy")

<nav class="mod_nav">
    <div class="lay_left">
        <a class="nav_back" onclick="turnBack()"><img src="/images/return_1.png" ontouchstart="replaceBackImage(this, 1)" ontouchend="replaceBackImage(this, 2)"></a>
        <span>购买</span>
    </div>
</nav>
<div class="buyWindow">
    <section class="OrderCon">
        <section id="defaultAddress" class="OrderPartOne">
            <div class="OrderPostion" id="validDelliveryAddr">
                <p><span id="deliveryAddrUserName"></span></p>
                <p id="deliveryAddr"></p>
            </div>
        </section>
        <section class="OrderPartTwo">
            <div class="OrderPay">
                <span>微信支付</span>
                <img id="weixinPay" paytypeid="2" src="@Url.Content("~/")images/OrderPayT.png">
            </div>
        </section>
        <section class="OrderPartThree">
            <div class="GoodsListLayout">
                <div class="goodsListDoc">
                    <table>
                        <tbody id="shoppingGoodsList"></tbody>
                    </table>
                </div>
            </div>
        </section>
    </section>
</div>
<section class="Shopping">
    <section class="ShopPart4">
        <div class="ShoppingTxt fll">
            <div class="ShoppingMoney1">
                <span>共</span><a id="ShoppingCarTotalPrice"></a>
            </div>
        </div>
        <div class="ShoppingAll flr" onclick="PayOrder()">
            <a><span>去结算</span></a>
        </div>
    </section>
</section>

<script>
    window.onload = function () {
        loadDeliveryAddr();
        showShoppingGoodsList();
        showShoppingCarFee();
        setBuyWindowHeight();
        OrderMgr.setOrderChangeNotifyCallback(orderChangeNotifyCallback);
    }

    //没有看懂这块的逻辑，只是这种内容和common.js无关，先移到这里来。
    $(function () {
        $('.OrderPay').each(function (index) {
            $('.OrderPay').eq(index).on('click', function () {
                $('.OrderPay img').attr('src', '/images/OrderPayF.png');
                $('.OrderPay img').attr('select', '0');
                $('.OrderPay img').eq(index).attr('src', '/images/OrderPayT.png');
                $('.OrderPay img').eq(index).attr('select', '1');
            });
        });
        var $OrderNum;
        $('.OrderPartThree .OrderAdd').click(function () {
            $OrderNum = parseInt($(this).parent().children('span').text());
            $OrderNum = $OrderNum + 1;
            $(this).parent().children('span').text($OrderNum);
        })
        $('.OrderPartThree .OrderPlus').click(function () {
            $OrderNum = parseInt($(this).parent().children('span').text());
            $OrderNum = $OrderNum - 1;
            if ($OrderNum == 0) {
                $(this).parent().parent().parent().remove();
            }
            $(this).parent().children('span').text($OrderNum);
        });
    })

    function turnBack() {
        OrderMgr.resetOrderChangeNotifyCallback();
        window.location.href = '/Home/Index';
    }

    function orderChangeNotifyCallback(orderInfo) {
        OrderMgr.resetOrderChangeNotifyCallback();
        //这里做变动（直接跳转到订单页面）
    }

    function loadDeliveryAddr() {
        var deliveryAddrInfo = TerminalMgr.getTerminalInfo();
        if (null != deliveryAddrInfo) {
            $("#deliveryAddrUserName").show();
            $("#deliveryAddrUserName").text(deliveryAddrInfo.name);
            $("#deliveryAddr").text(deliveryAddrInfo.address);
        }
    }
    //配送费、总价、邮费
    function showShoppingCarFee() {
        var totalPrice = ShoppingMgr.getTotalPrice();
        $('#ShoppingCarTotalPrice').html("￥" + totalPrice.toFixed(1));

    }

    function showShoppingGoodsList() {
        var shoppingList = ShoppingMgr.getShoppingList();
        if (null != shoppingList && shoppingList.length > 0) {
            var url = '/Buy/ShowGoodsInfo';
            NativeAjax(shoppingList, url, function (msg) { loadPartialViewResult(msg, $("#shoppingGoodsList"), 1); });
        }
    }

    function getOrderPayType() {
        var aa = $("#weixinPay")[0].src;
        if (aa.indexOf("OrderPayT.png") > 0) {
            return OrderMgr.weixinPayType;
        } else {
            return OrderMgr.jifenPayType;
        }
    }

    function validOrderCallback(orderPayInfo) {
        //这里表示的是有效订单，接下来就是要支付
        //微信支付，待确定
        var payType = orderPayInfo.pay_type;
        if (payType == OrderMgr.jifenPayType) {//积分支付
            window.onload.herf = '/OrderInfo/Index';
        } else if (payType == OrderMgr.weixinPayType) {//微信支付
            order_info_tag_json = JSON.parse(orderPayInfo.order_pay_info.order_info_tag);
            var orderId = orderPayInfo.order_pay_info.order_id;
            var totleFee = order_info_tag_json.total_fee;
            var userOrderNumber = order_info_tag_json.attach;
            var attachInfo = totleFee + "|" + userOrderNumber;//这里可以带信息去微信服务器 支付成功后会原样返回
            window.location.href = '/TenPayV3/Index?attachInfo=' + attachInfo
                + '&orderNumber=' + orderPayInfo.order_pay_info.order_id + "&showwxpaytitle=1";
        }
        ShoppingMgr.resetShoppingMgr();
    }
    function invalidOrderCallback() {
        //这里表示是无效订单，需要刷新商品列表
        showShoppingGoodsList();
        showShoppingCarFee();
    }

    function PayOrder() {
        //先判断是否登录，送货地址是否有效，购物车中是否有商品
        //ShoppingMgr.preHandleOrder，记得参数中设置上面的回调接口
        if (UserInfoMgr.isLogIn()) {
            var shoppingList = ShoppingMgr.getShoppingList();
            if (shoppingList != null && shoppingList.length > 0) {
                ShoppingMgr.preHandleOrder(getOrderPayType(), validOrderCallback, invalidOrderCallback);
            } else {
                venderDialog("您未选购任何商品，请选购商品后再下单！");
            }
        } else {
            window.location.href = spliceRegisterUrl('/Buy/Index', '/Home/Index');
        }
    }
    function setBuyWindowHeight() {
        //当前浏览器窗口高度 - 购物车高度 Nav高度
        var currentWindowHeight = $(window).height() - 56 - 48;
        //当前容器确定高度
        $(".buyWindow").css("height", currentWindowHeight + "px");
    }
</script>